import { _decorator, NodeEventType, Node, Sprite, Color, Label } from 'cc';
import { UIComponent } from '../Base/UIComponent';
import { EntityTypeEnum, GAttribute, GTalent } from '../Common';
import { TalentManager, TalentStateEnum } from '../Runtime/TalentManager';
import { LightColorEnum, PanelColorEnum } from '../Enum';
import { IconBaseColorEnum } from '../Enum';
import { IconLightColorEnum } from '../Enum';
import BagManager from '../Runtime/BagManager';
const { ccclass, property } = _decorator;

@ccclass('TalentUI')
export class TalentUI extends UIComponent {

    talentList: Node
    /** 当前选择的科技 */
    curSelectId: number = 1
    /** 科技名字 + 等级 */
    labName: Label
    /** 科技描述 */
    labDesc: Label
    /** 升级消耗 */
    labCost: Label
    /** 当前拥有 */
    labScrollNum: Label

    scroll: Node
    btnLvlUp: Node
    onLoad() {
        this.talentList = this.node.getChildByName('talentList')
        const detailNode = this.node.getChildByName('detailNode')

        this.labName = detailNode.getChildByName('labName').getComponent(Label)
        this.labDesc = detailNode.getChildByName('labDesc').getComponent(Label)
        this.labCost = detailNode.getChildByName('labCost').getComponent(Label)
        this.labScrollNum = this.node.getChildByName('labScrollNum').getComponent(Label)

        const btnBack = this.node.getChildByName('btnBack')
        const btnLvlUp = this.btnLvlUp = detailNode.getChildByName('btnLvlUp')
        this.scroll = detailNode.getChildByName('scroll')

        btnBack.on(NodeEventType.TOUCH_END, () => {
            this.onBtnBack()
        })
        btnLvlUp.on(NodeEventType.TOUCH_END, () => {
            TalentManager.Ins.lvlUp(this.curSelectId)
            this.refresh()
        })

    }

    show() {
        this.setMaskEffect()
        this.node.active = true
        this.refresh()
    }

    onBtnBack() {
        this.node.active = false
    }

    refresh() {

        this.refreshList()

        const config = GTalent[this.curSelectId]
        const curLvl = TalentManager.Ins.talentData[this.curSelectId] || 0
        const name = this.getTalentNameById(this.curSelectId)
        this.labName.string = name + ' lv.' + curLvl
        this.labDesc.string = config.desc
        this.labCost.string = '' + config.cost
        const myTalentCurrencyNum = BagManager.Ins.getNumberById(EntityTypeEnum.scroll)
        this.labScrollNum.string = myTalentCurrencyNum
        if (myTalentCurrencyNum < config.cost) {
            this.labCost.color = Color.RED
        } else {
            this.labCost.color = Color.WHITE
        }


        const talentState = TalentManager.Ins.getTalentStateById(this.curSelectId)
        switch (talentState) {
            case TalentStateEnum.maxLvl:
                this.btnLvlUp.active = false
                this.labCost.node.active = false
                this.scroll.active = false
                break;
            case TalentStateEnum.unlock:
                this.btnLvlUp.active = true
                this.labCost.node.active = true
                this.scroll.active = true
                break;
            case TalentStateEnum.lock:
                this.btnLvlUp.active = true
                this.labCost.node.active = true
                this.scroll.active = true
            default:
                break;
        }
    }

    /** 刷新科技列表 */
    refreshList() {

        for (const id in GTalent) {
            const item = this.talentList.getChildByName(id)
            const panel = item.getChildByName('panel').getComponent(Sprite)
            const light = item.getChildByName('light').getComponent(Sprite)

            const talentState = TalentManager.Ins.getTalentStateById(id)
            switch (talentState) {
                case TalentStateEnum.maxLvl:
                    panel.color = new Color().fromHEX(PanelColorEnum.green)
                    light.color = new Color().fromHEX(LightColorEnum.green)
                    break;
                case TalentStateEnum.unlock:
                    panel.color = new Color().fromHEX(PanelColorEnum.yellow)
                    light.color = new Color().fromHEX(LightColorEnum.yellow)
                    break;
                case TalentStateEnum.lock:
                default:
                    panel.color = new Color().fromHEX(IconBaseColorEnum.normal)
                    light.color = new Color().fromHEX(IconLightColorEnum.normal)
                    break;
            }

            const labName = item.getChildByName('labName').getComponent(Label)

            labName.string = this.getTalentNameById(id)

            if (this.curSelectId == Number(id)) {
                light.color = new Color().fromHEX(LightColorEnum.orange)
            }

            item.off(NodeEventType.TOUCH_END)
            item.on(NodeEventType.TOUCH_END, () => {
                this.curSelectId = Number(id)
                this.refresh()
            })

        }
    }

    /** 获得科技名字 */
    getTalentNameById(id: any) {
        const config = GTalent[id]
        let name = config.name
        if (!name) {
            name = GAttribute[config.attrType].name
        }
        return name
    }
}